package com.client.koshercook.server;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.urlfetch.HTTPRequest;

public class ServerInit extends HttpServlet{

	private static final long serialVersionUID = 1898906064199177235L;

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws IOException {

	doLogic();
	}
	public void doGet(HttpServletRequest req,HttpServletResponse res)
	throws IOException {
	doPost( req, res);
	}
	/**scan new recipes to DS*/
	public void doLogic(){
		
		//get the id of candidates to new recipes
		Globals globals = DataStore.getGlobals();
		
		//a new week for popular
		if(globals.getWeekNumber()==null) globals.initPopularRecipesArrays();
		globals.resetViews(); //TODO remove from comment only when we finish scan manually
		DataStore.updateGlobals(globals);
		
		//scanning new recipes
		int startAt;
		String site;
		if(Debug.SCANSITE == 1) {
			site = "http://www.kipa.co.il/food/matkon/";
			startAt= globals.getLastRecKipa() + 1;
		}
		else if(Debug.SCANSITE == 2) {
			site = "http://www.chef-lavan.co.il/tabid/53/RecipeID/";
			startAt= globals.getLastRecChefLavan() + 1;
		}
		else if(Debug.SCANSITE == 3) {
			site = "http://www.rotev.co.il/last_video.aspx?recipe_id=";
			startAt= globals.getLastRecRotev() + 1;
		}
		else if(Debug.SCANSITE == 4) {
			site = "http://www.stybel.co.il/recipe_full.php?id=";
			startAt= globals.getLastRecStybel() + 1;
		}
		
//		startAt = 45;//242; //for manual choosing
		
		int maxScans = Debug.MAXSCANS; //max number of recipes to scan from each site
		int tries=Debug.TRIES , fail=0;
		
		
		
		
		System.out.println("starting to harvest data from: "+site);

		Recipes rec;
		int i, success=0;
		for(i=startAt ; success<maxScans ; i++) {
			System.out.print("init: adding recipe "+i+".. ");
			
			rec = new Recipes(site,i);
			System.out.println("init: created a new recipe");
			if(!rec.isValid()){
				System.out.println("init: recipe "+i+" not valid. ");
				fail++;
				if(fail==tries) {
	//				i++;
					if(Debug.ADVANCE_SCAN) {
						if(Debug.SCANSITE == 1) globals.setLastRecKipa(i);
						else if(Debug.SCANSITE == 2) globals.setLastRecChefLavan(i);
						else if(Debug.SCANSITE == 3) globals.setLastRecRotev(i);
						else if(Debug.SCANSITE == 4) globals.setLastRecStybel(i);
						DataStore.updateGlobals(globals);
						System.out.println("too many fails");
					}
					break;  //in case there are no more Recipes on the site
				}
			}
			else { 
	//			fail=0;
				DataStore.addRecipe(rec);
				success++;
//				if(rec.hasAnyAlters()) DataStore.addGroupToRecipe(rec.getKey(),rec);
				if(Debug.SCANSITE == 1) globals.setLastRecKipa(i);
				else if(Debug.SCANSITE == 2) globals.setLastRecChefLavan(i);
				else if(Debug.SCANSITE == 3) globals.setLastRecRotev(i);
				else if(Debug.SCANSITE == 4) globals.setLastRecStybel(i);
				globals.setNumOfRecipesInDS(globals.getNumOfRecipesInDS()+1);
				DataStore.updateGlobals(globals);
				System.out.println("added");
			}
		}
		
		
	}
	
}
